package com.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.constant.MessageConstant;
import com.exception.CustomException;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mapper.RoleMapper;
import com.pageResult.PageResult;
import com.pojo.Role;
import com.pojo.User;
import com.query.QueryPageBean;
import com.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = RoleService.class)
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public List<Role> queryAll() {
        return roleMapper.selectByExample(null);
    }


    @Override
    public PageResult selectByQueryString(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),true);
        Page<Role> page = roleMapper.selectByQueryString(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    @Transactional
    public void delete(Integer id) throws CustomException {
        Integer count = roleMapper.queryByRoleId(id);
        if(count>0){//如果该角色有权限在引用则抛出异常
            throw new CustomException(MessageConstant.ROLE_IN_USE);
        }
        Integer count1 = roleMapper.queryByUserCount(id);
        if(count1>0){//如果该角色有用户在引用则抛出异常
            throw new CustomException(MessageConstant.ROLE_IN_USEE);
        }
        Integer count2 = roleMapper.queryByMenuCount(id);
        if(count2>0){//有没有菜单在引用此角色
            throw new CustomException(MessageConstant.ROLE_IN_USEEE);
        }
        //否则删除
        roleMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public void add(Role role, Integer[] PermissionId,Integer [] MenuIds) {
        //先添加用户数据
        roleMapper.insertSelective(role);
        //在添加用户和角色第三方表数据
        if(PermissionId!=null && PermissionId.length>0){//不为空的情况下
            roleMapper.addRoleAndPermission(role.getId(),PermissionId);
        }
        //在添加用户和菜单第三方表数据
        if(MenuIds!=null && MenuIds.length>0){
            roleMapper.addRoleAndMenu(role.getId(),MenuIds);
        }
    }

    @Override
    public Role queryById(Integer id) {
        return roleMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer[] queryByPermissionId(Integer RoleId) {
        return roleMapper.queryByPermissionId(RoleId);
    }

    @Override
    public Integer[] queryByMenuId(Integer RoleId) {
        return roleMapper.queryByMenuId(RoleId);
    }

    @Override
    @Transactional
    public void update(Role role, Integer[] PermissionId,Integer [] MenuIds) {
        //先修改用户数据
        roleMapper.updateByPrimaryKeySelective(role);
        if(PermissionId!=null && PermissionId.length>0){//不为空的情况下
            //1.先删除在重新添加
            roleMapper.deleteByRole(role.getId());
            //添加
            roleMapper.addRoleAndPermission(role.getId(),PermissionId);
        }else {
            roleMapper.deleteByRole(role.getId());
        }

        if(MenuIds!=null && MenuIds.length>0){//不为空的情况下
            //1.先删除在重新添加
            roleMapper.deleteByRoleMenu(role.getId());
            //添加
            roleMapper.addRoleAndMenu(role.getId(),MenuIds);
        }else {
            roleMapper.deleteByRoleMenu(role.getId());
        }


    }
}
